package com.apotik.legenda.dialog;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.JButton;

import com.apotik.legenda.dao.InsuranceDao;
import com.apotik.legenda.model.Insurance;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.GeneralObserver;
import com.apotik.legenda.util.CalendarUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Vector;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.border.EmptyBorder;

import java.awt.BorderLayout;

import javax.swing.border.MatteBorder;

import java.awt.GridLayout;

public class AddEditInsuranceDialog extends JDialog implements ActionListener, KeyListener{
	private static final long serialVersionUID = 1L;

	private JPanel panel;
	
	private JTextField textFieldNama;
	
	private JButton btnSave;
	private JButton btnCancel;
	
	private User user;
	private GeneralObserver observer;
	private Insurance currentInsurance;
	private int activePageIndex = -1;
	
	
	public AddEditInsuranceDialog(String title, User user, GeneralObserver observer) {
		this.user = user;
		this.observer = observer;
		
		setTitle(title);
		setSize(387, 240);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
					AddEditInsuranceDialog.this.setVisible(false);
				} else if(e.getKeyCode()==KeyEvent.VK_ENTER){
					
				}
			}
		});
		
		
		panel = new JPanel();
		panel.setBorder(new EmptyBorder(7, 7, 7, 7));
		getContentPane().add(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_5 = new JPanel();
		panel.add(panel_5, BorderLayout.NORTH);
		panel_5.setLayout(new GridLayout(0, 2, 0, 5));
		
		JLabel lblNama = new JLabel("Nama Asuransi");
		panel_5.add(lblNama);
		
		textFieldNama = new JTextField();
		panel_5.add(textFieldNama);
		textFieldNama.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new MatteBorder(1, 0, 0, 0, (Color) Color.GRAY));
		panel_1.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new EmptyBorder(3, 3, 3, 3));
		panel_2.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_4 = new JPanel();
		panel_3.add(panel_4, BorderLayout.EAST);
		
		btnSave = new JButton("Save");
		btnSave.addActionListener(this);
		btnSave.addKeyListener(this);
		panel_4.add(btnSave);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(this);
		btnCancel.addKeyListener(this);
		panel_4.add(btnCancel);	
	}
	
	private void btnSubmitAction(){
		String nama = textFieldNama.getText();
		
		Insurance o = null;
		if(currentInsurance!=null){
			o = currentInsurance;
		}else{
			o = new Insurance();
			o.setCreatedBy(user);
			o.setCreatedDate(CalendarUtil.dateNow());
			o.setActive(true);
		}
		o.setInsuranceName(nama);
		
		InsuranceDao dao = new InsuranceDao();
		if(dao.saveOrUpdate(o)){
			JOptionPane.showMessageDialog(null, "Sukses", "Success", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(null, "Gagal", "Error", JOptionPane.ERROR_MESSAGE);
		}

		
		HashMap<String, Object> hm = new HashMap<>();
		hm.put("activePageIndex", activePageIndex);
		observer.response(hm);
		
		this.setVisible(false);
	}
	
	@SuppressWarnings("rawtypes")
	public void fillInTheFields(Vector vectorRow, int activePageIndex){
		if(vectorRow!=null){
			this.activePageIndex = activePageIndex;
			this.currentInsurance = (Insurance) vectorRow.get(vectorRow.size()-1);
			
			textFieldNama.setText(currentInsurance.getInsuranceName());
		}
	}
	
	@SuppressWarnings({ })
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		
		if(obj==btnSave){
			btnSubmitAction();
		}else if(obj==btnCancel){
			this.setVisible(false);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
			this.setVisible(false);
		}
		
		Object source = e.getSource();
		if(source==btnSave && e.getKeyCode()==KeyEvent.VK_ENTER){
			btnSubmitAction();
		}else if(source==btnCancel && e.getKeyCode()==KeyEvent.VK_ENTER){
			this.setVisible(false);
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}
